package com.order.server.config;

import com.alibaba.cloud.sentinel.annotation.SentinelRestTemplate;
import com.order.server.RestTemplateExceptionHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

import java.util.Collections;

@Configuration
public class OrderServerConfig {

    @Autowired
    LoadBalancerClient loadBalancerClient;

    @Bean
    @SentinelRestTemplate(blockHandlerClass = RestTemplateExceptionHandler.class, blockHandler = "handleBlock",
            fallbackClass = RestTemplateExceptionHandler.class, fallback = "fallBack")
    public RestTemplate restTemplate(){
        RestTemplate restTemplate = new RestTemplate();
        restTemplate.setInterceptors(Collections.singletonList(new LoadBalancerInterceptor(loadBalancerClient)));
        return restTemplate;
    }

}
